package com.mmlc.meimei.lichengkongjian.home;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.androidnetframe.httputil.BaseRequestFilterLayer;
import com.androidnetframe.httputil.EntryPageName;
import com.androidnetframe.requestbean.GlobalRequestBean;
import com.androidnetframe.responsejavabean.RequestError;
import com.androidnetframe.utils.LogUtils;
import com.mmlc.inter.InterActivity;
import com.mmlc.inter.ViewPagerFragmentAdapter;
import com.mmlc.meimei.lichengkongjian.R;
import com.mmlc.meimei.lichengkongjian.customview.LabelFlowLayout;
import com.mmlc.network.filter.NewProductListRequestFilter;
import com.mmlc.network.responsebean.NewProductListResponseBean;
import com.mmlc.utils.AppUtils;
import com.mmlc.utils.ViewUtils;
import com.mmlc.widget.Indicator;

import java.util.ArrayList;
import java.util.List;

public class NewProductActivity extends InterActivity {

    ViewPager vp_fashion;
    ArrayList<Fragment> fragments;
    FrameLayout fr_label;
    LabelFlowLayout ll_labels;

    private Indicator mIndicator;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int initContentView() {
        return R.layout.activity_new_product;

    }

    @Override
    protected void initView() {
        vp_fashion = ViewUtils.getView(activity, R.id.vp_new_product);
        mIndicator = ViewUtils.getView(activity, R.id.indicator_v);
        fr_label = ViewUtils.getView(this,R.id.fr_label);
        ll_labels = ViewUtils.getView(this, R.id.ll_labels);
        fragments = new ArrayList<>();
        ViewUtils.getView(activity, R.id.iv_back).setOnClickListener(this);

        List<String> titles = new ArrayList<String>();
        for (GlobalRequestBean.DataBean.RecommendBean.NewAddBean homeBean : EntryPageName
                .globalRequestBean.getData().getRecommend().getNew_add()) {
            if (TextUtils.equals(homeBean.getType(), "0")) {
                titles.add(homeBean.getName());
                Fragment fragment = new NewpoductListFragment();
                Bundle bundle = new Bundle();
                id =  homeBean.getId();
                bundle.putString("id", homeBean.getId());
                fragment.setArguments(bundle);
                fragments.add(fragment);
            }
        }
        if (titles.size() > 0)
        {
            mIndicator.setTitles(titles);
            mIndicator.setVisibility(View.VISIBLE);
            vp_fashion.setAdapter(new ViewPagerFragmentAdapter(getSupportFragmentManager(), fragments));
            mIndicator.setmViewPager(vp_fashion, 0);
        }
        vp_fashion.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                List<GlobalRequestBean.DataBean.RecommendBean.NewAddBean> newAdd = EntryPageName
                        .globalRequestBean.getData().getRecommend().getNew_add();
                String id = newAdd.get(position).getId();
                initLabel(id);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
//        mIndicator.setIndexChangeListener(new Indicator.onIndexChangeListener() {
//            @Override
//            public void onIndexSelected(int index) {
//                List<GlobalRequestBean.DataBean.RecommendBean.NewAddBean> newAdd = EntryPageName
//                        .globalRequestBean.getData().getRecommend().getNew_add();
//                String id = newAdd.get(index).getId();
//                initLabel(id);
//            }
//        });

        if(null!=EntryPageName.globalRequestBean.getData().getRecommend().getNew_add()&&
                EntryPageName.globalRequestBean.getData().getRecommend().getNew_add().size()>0){
            String id = EntryPageName.globalRequestBean.getData().getRecommend().getNew_add().get(0).getId();
            initLabel(id);
        }

    }
    String id;
    List<String> labels;
    public void initLabel(String id){
        LogUtils.e("initLabel","是执行了的");
        NewProductListRequestFilter filter=new NewProductListRequestFilter(activity);
        filter.requestBean.recommend_id= id;
        filter.requestBean.version = AppUtils.getVersionCode(this)+"";
        filter.sendRequest(new BaseRequestFilterLayer.AccessResultFromFilter<NewProductListResponseBean>() {

            @Override
            public void onSuccess(NewProductListResponseBean result) {
                if(result!=null && result.getData()!=null && result.getData().getLabels()!=null
                        && result.getData().getLabels().size()>0){
                    labels = result.getData().getLabels();
                    addLabel();
                }else{
                    fr_label.setVisibility(View.GONE);
                }

            }

            @Override
            public void onFailed(View errorLayout, RequestError serverError, int failedType) {

            }
        });
    }

    private void addLabel() {
        fr_label.setVisibility(View.VISIBLE);
        if(ll_labels.getChildCount()>0){
            ll_labels.removeAllViews();
        }
        for(int i = 0; i < labels.size(); i ++){
            View view = LayoutInflater.from(this).inflate(R.layout.item_tag, ll_labels,false);
            TextView tv_tag = (TextView) view.findViewById(R.id.tv_tag);
            tv_tag.setText(labels.get(i));
            ll_labels.addView(view);
        }
    }

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }
}
